package com.jy.wms.service;

import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsDistributionMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.List;

/**
 * @author yjs
 * @ClassName: WmsDistributionService.java
 * @Description: 发运订单-分配
 * @Date Tue Oct 24 13:54:03 GMT+08:00 2017
 */

@Service
public class WmsDistributionService extends BaseService<WmsDistributionMapper, WmsDistribution> {
    /**
     * 发运订单-分配管理
     *
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "分配操作", btnName = "分配操作")
    @Transactional
    public void distribution(List<String> list, UserInfo userinfo, String hzid) throws Exception {
        Map<String, String> map = new HashMap<>();
        //取出当前用户
        User user = userinfo.getUser();
        //分配总数量
        Integer fpZsl = 0;
        //仓库编号
        String ckId = String.valueOf(userinfo.getUser().getOrgId().get(0));
        //分配实体类
        WmsDistribution wmsDistribution = null;
        //分配明细(历史)实体类
        WmsDistributionHistory wmsDistributionHistory = null;
        //库存数量对象
        List<WmsStock> listStrokes = new ArrayList<>();
        //查询货主表(数量不足是否允许出库 0不允许 1允许)
        WmsOwner wmsOwner = dao.searchHzSlbuck(hzid);
        //查询周转规则排序(用货主编号通过货主与周转规则中间表排序显示规则字段)
        String zzGz = dao.searchZzgz(hzid, ckId);
        //标记专用(0,未进;1,进入)
        String bjzy = "0";
        //存储库位所对应的属性
        List<String> sxList = null;
        //存储库位所对应的类型(第一种情况使用)
        List<String> lxList = null;
        //存储库位所对应的类型(第二种情况使用)
        List<String> lxList2 = null;
        //统计发运订单货品数量为空的订单
        StringBuilder stringBuilder = null;
        //循环发运订单数据
        for (String id : list) {
            //用于判断发运订单明细状态初始数量
            Integer cssl = 0;
            //查询发运订单数据
            WmsShipmentOrder wmsShipmentOrder = dao.searchOrder(id);
            //发运订单状态为初始
            String faZt = "0";
            if (!faZt.equals(wmsShipmentOrder.getFyzt())) {
                throw new ServiceException("请选择SO状态为初始的数据");
            } else {
                //已审核
                String shZt = "1";
                if (!shZt.equals(wmsShipmentOrder.getShzt())) {
                    throw new ServiceException("请选择发运订单已审核的数据");
                } else {
                    //已拆单
                    String cdZt = "1";
                    if (!cdZt.equals(wmsShipmentOrder.getCdzt())) {
                        throw new ServiceException("请选择发运订单已拆单的数据");
                    } else {
                        //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                        String jhrwzt = "0";
                        if (!jhrwzt.equals(wmsShipmentOrder.getJhrwzt())) {
                            throw new ServiceException("请选择发运订单拣货任务初始的数据");
                        } else {
                            //0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成 9 代表空
                            String ddZt = "0";
                            //查询发运订单明细
                            List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(wmsShipmentOrder.getId(), "");
                            //循环发运订单明细数据
                            for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                                //查询拆单信息()
                                List<WmsSplit> listWmsSplit = dao.searchWmsSplit(wmsShipmentOrderDetailed.getId());
                                //包装类型所对应的主单位
                                String zdWsl = null;
                                //循环拆单信息
                                for (WmsSplit wmsSplit : listWmsSplit) {
                                    //标记库位是否为空(false为空)
                                    boolean flags = false;
                                    //标记批次是否为空(false为空)
                                    boolean flagPc = false;
                                    //标记日期是否为空(false为空)
                                    boolean flagRq = false;
                                    //存储库位所对应的属性
                                    sxList = new ArrayList<>();
                                    //判断库位是否为空
                                    if (wmsSplit.getKwid() != null && !("").equals(wmsSplit.getKwid())) {
                                        // 0正常 1封存 2管控 3禁入
                                        sxList.add("0");
                                        sxList.add("2");
                                        sxList.add("3");
                                        flags = true;
                                    } else {
                                        // 0正常 1封存 2管控 3禁入
                                        sxList.add("0");
                                        sxList.add("3");
                                        flags = false;
                                    }
                                    //存储库位所对应的类型(4暂存库位 5集货库位 不可以分配)
                                    lxList = new ArrayList<>();
                                    //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
                                    lxList.add("4");
                                    lxList.add("5");
                                    String pc = wmsSplit.getPc();
                                    if (!"".equals(pc) && pc != null) {
                                        flagPc = true;
                                    }
                                    String scrq = wmsSplit.getScrq();
                                    if (!"".equals(scrq) && scrq != null) {
                                        flagRq = true;
                                    }
                                    //判断库位,批次,生产日期是否为空
                                    if (flags == true | flagPc == true | flagRq == true) {
                                        //库位,批次,生产日期,如果有一个不为空,就执行下面的方法
                                        List<WmsStock> listStroke = dao.searchWmsStock(wmsSplit.getKwid(), wmsSplit.getPc(), wmsSplit.getScrq(), wmsSplit.getHpid(), hzid, ckId, sxList, zzGz, lxList);
                                        if (listStroke.size() > 0) {
                                            map = searchOne(wmsSplit, listStroke, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                            if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                fpZsl = Integer.parseInt(map.get("fpZsl"));
                                            } else {
                                                fpZsl = 0;
                                            }
                                        } else {
                                            //没有库存的时候跳出当前循环
                                            continue;
                                        }
                                    } else {
                                        //分配规则(0:完全按照拣货库位类型;1:整箱优先找指定拣货库位类型);第一种情况
                                        if ("0".equals(wmsOwner.getFpgz()) || !"2".equals(wmsSplit.getBzlx())) {
                                            //库位,批次,生产日期,如果都为空,就执行下面的方法
                                            //查询拣货库位表对应的库位(货品ID,包装类型,货主实体类,仓库ID)
                                            String jhBmx = jhBmx(wmsSplit.getBzlx());
                                            List<WmsStock> wmsStockList = dao.searchKC3(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList);
                                            if (wmsStockList.size() > 0) {
                                                map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                    fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                } else {
                                                    fpZsl = 0;
                                                }
                                            } else {
                                                //没有库存的时候跳出当前循环
                                                continue;
                                            }
                                        } else if ("1".equals(wmsOwner.getFpgz()) && "2".equals(wmsSplit.getBzlx())) {
                                            //查询拣货库位表对应的库位(货品ID,包装类型,货主实体类,仓库ID)第二种情况
                                            String jhBmx = jhBmx(wmsSplit.getBzlx());
                                            //查询货品对应的拣货库位类型(根据包装类型找到对应的拣货库位表,结合拆单表内的货品ID)
                                            String kwlx = dao.searchJhlx(wmsSplit.getHpid(), ckId, jhBmx);
                                            //库位类型集合
                                            lxList2 = kwLx(kwlx, lxList2);
                                            //查询该货品批次集合(存储库位,箱拣货库位,箱/件拣货库位,件拣货库位)
                                            List<String> hppcList = dao.searchHppc(wmsSplit.getHpid(), hzid, ckId);
                                            //拆单数量
                                            Integer cdSl = Integer.valueOf(wmsSplit.getSl());
                                            //累加库存数量时专用
                                            Integer countList2 = 0;
                                            go:
                                            for (String hppc2 : hppcList) {
                                                //统计库存内的可用数量(指定库位类型)
                                                Integer countList1 = dao.countKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, kwlx);
                                                //判断指定库位类型的数量是否足够
                                                if (countList1 < cdSl) {
                                                    //指定库位类型的数量不足时执行
                                                    for (String lx : lxList2) {
                                                        Integer countList = dao.countKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, lx);
                                                        if (countList < cdSl) {
                                                            //单个库位类型下面的库存数量不足,继续循环下一种库位类型
                                                            //所有库位类型库存数量叠加
                                                            countList1 += countList;
                                                        } else {
                                                            //库位类型下面的库存数量足够,执行
                                                            List<WmsStock> wmsStockList = dao.searchKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, lx);
                                                            if (wmsStockList.size() > 0) {
                                                                map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                                if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                                    fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                                } else {
                                                                    fpZsl = 0;
                                                                }
                                                                break go;
                                                            }
                                                        }
                                                    }
                                                    countList2 += countList1;
                                                } else {
                                                    //指定库位类型的数量足够时执行
                                                    List<WmsStock> wmsStockList = dao.searchKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, kwlx);
                                                    if (wmsStockList.size() > 0) {
                                                        map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                        if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                            fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                        } else {
                                                            fpZsl = 0;
                                                        }
                                                        break go;
                                                    }
                                                }
                                            }
                                            if (countList2 < cdSl) {
                                                //所有货品批次,库位类型内的库存数量都不足时执行
                                                if ("0".equals(wmsOwner.getSlbuck())) {
                                                    throw new ServiceException("库存数量不足,不允许出库");
                                                } else {
                                                    //数量不足,允许出库(进行分配)
                                                    List<WmsStock> wmsStockList = dao.searchKC4(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppcList, lxList2);
                                                    if (wmsStockList.size() > 0) {
                                                        map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                        if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                            fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                        } else {
                                                            fpZsl = 0;
                                                        }
                                                    }
                                                }
                                            } else {
                                                //所有货品批次,库位类型内的库存数量都足够时执行
                                                //数量足够,允许出库(进行分配)
                                                List<WmsStock> wmsStockList = dao.searchKC4(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppcList, lxList2);
                                                if (wmsStockList.size() > 0) {
                                                    map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                    if (!("").equals(map.get("fpZsl")) && map.get("fpZsl") != null) {
                                                        fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                    } else {
                                                        fpZsl = 0;
                                                    }
                                                }
                                            }
                                        } else {
                                            throw new ServiceException("货主分配规则有误");
                                        }
                                    }
                                }
                                //(分配）箱单位数量==>2代表箱;取包装明细箱类型的数量
                                WmsPackingDetailed wmsPackingDetailed = dao.searchPackingDetailed("2", wmsShipmentOrderDetailed.getHpid(), hzid);
                                //包装明细主单位类型的数量
                                WmsPackingDetailed wmsPackingDetailed0 = dao.searchPackingDetailed("0", wmsShipmentOrderDetailed.getHpid(), hzid);
                                zdWsl = wmsPackingDetailed.getZdwsl();
                                //箱主单位整数（箱数）
                                Integer xdWzs = fpZsl / Integer.parseInt(zdWsl);
                                //箱主单位余数（个数）
                                Integer xdWys = fpZsl % Integer.parseInt(zdWsl);
                                String zdWsl2 = xdWzs + wmsPackingDetailed.getMs() + xdWys + wmsPackingDetailed0.getMs();
                                //发运订单明细状态
                                String ddmXzt = null;
                                //明细预期主单位数量
                                Integer mxYqSl = Integer.parseInt(wmsShipmentOrderDetailed.getYqsl());
                                //发运订单明细状态(0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成)
                                if ("1".equals(map.get("bjZy"))) {
                                    if (fpZsl.equals(0)) {
                                        //初始(没有库存是执行)
                                        ddmXzt = "0";
                                        cssl++;
                                    } else if (fpZsl < mxYqSl) {
                                        //部分分配
                                        ddmXzt = "1";
                                    }
                                } else if ("2".equals(map.get("bjZy"))) {
                                    if (fpZsl.equals(mxYqSl)) {
                                        //全部分配
                                        ddmXzt = "2";
                                    }
                                } else if ("0".equals(map.get("bjZy"))) {
                                    //初始
                                    ddmXzt = "0";
                                    cssl++;
                                } else if ("".equals(map.get("bjZy")) || map.get("bjZy") == null) {
                                    //初始
                                    ddmXzt = "0";
                                    cssl++;
                                }
                                Integer fymCzt = dao.updateOrderDetail(wmsShipmentOrderDetailed.getId(), ddmXzt, zdWsl2, String.valueOf(fpZsl));
                                if (fymCzt <= 0) {
                                    throw new ServiceException("发运订单明细状态更改失败");
                                }
                            }
                            //查询分配是否多批次
                            List<WmsDistribution> wmsDistributionList = dao.searchPcList(wmsShipmentOrder.getId(), hzid, ckId);
                            //定义批次状态0：多批次 1：单批次
                            String dpczt = "0";
                            if (wmsDistributionList.size() > 1) {
                                dpczt = "0";
                            } else {
                                dpczt = "1";
                            }
                            //修改发运单状态(传入发运订单ID),0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成
                            if (cssl.equals(wmsShipmentOrderDetailedList.size())) {
                                stringBuilder.append(wmsShipmentOrder.getFybh());
                                stringBuilder.append(",");
                                ddZt = "0";
                            } else if ("1".equals(map.get("bjZy"))) {
                                //0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成 9 代表空
                                ddZt = "1";
                            } else {
                                //0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成 9 代表空
                                ddZt = "2";
                            }
                            Integer fyZt = dao.updateOrder(wmsShipmentOrder.getId(), ddZt, dpczt, null, null, null);
                            if (fyZt <= 0) {
                                throw new ServiceException("发运订单状态更改失败");
                            }
                        }
                    }
                }
            }
        }
        if (stringBuilder != null) {
            throw new ServiceException(stringBuilder.toString() + "订单,库存不足分配");
        }
        throw new SucessException("分配操作成功");
    }

    /**
     * 添加分配信息
     * @param wmsDistribution
     * @param wmsSplit
     * @param wmsStocks
     * @param ckId
     * @return
     */
    public void baseInsert(WmsDistribution wmsDistribution, WmsSplit wmsSplit, WmsStock wmsStocks, String ckId) throws ServiceException {
        //分配ID
        wmsDistribution.setId(UUIDUtils.uuid());
        //拆单ID
        wmsDistribution.setSscdid(wmsSplit.getId());
        //发运明细ID
        wmsDistribution.setSsfymxid(wmsSplit.getSsfymxid());
        //货品ID
        wmsDistribution.setHpid(wmsSplit.getHpid());
        //查询货品对应的包装主单位明细
        WmsPackingDetailed wmsPackingDetailed = dao.searchPackingDetailed("0", wmsSplit.getHpid(), wmsStocks.getSshzid());
        //总重量=分配数量*货品主单位毛重量
        Double weight = 0.0;
        if (!"".equals(wmsPackingDetailed.getZl()) && wmsPackingDetailed.getZl() != null) {
            weight = Double.parseDouble(wmsStocks.getSl()) * Double.parseDouble(wmsPackingDetailed.getZl());
        }
        BigDecimal totalWeight = java.math.BigDecimal.valueOf(weight);
        wmsDistribution.setTotalWeight(totalWeight);
        //总体积=分配数量*货品主单位体积
        Double volume = 0.0;
        if (!"".equals(wmsPackingDetailed.getTj()) && wmsPackingDetailed.getTj() != null) {
            volume = Double.parseDouble(wmsStocks.getSl()) * Double.parseDouble(wmsPackingDetailed.getTj());
        }
        BigDecimal totalVolume = java.math.BigDecimal.valueOf(volume);
        wmsDistribution.setTotalVolume(totalVolume);
        //库存ID
        wmsDistribution.setKcid(wmsStocks.getId());
        //分配数量(库存表待下架数量=待拣货数量)
        wmsDistribution.setFpsl(wmsStocks.getSl());
        wmsDistribution.setCkid(ckId);
        Integer fpTj = dao.baseInsert(wmsDistribution);
        if (fpTj <= 0) {
            throw new ServiceException("分配历史信息添加失败");
        }
    }

    /**
     * 添加分配明细信息
     *
     * @param wmsDistributionHistory
     * @param wmsSplit
     * @param wmsStocks
     * @param name
     * @param ckId
     * @return
     */
    public void baseInsertDetail(WmsDistributionHistory wmsDistributionHistory, WmsSplit wmsSplit, WmsStock wmsStocks, String name, String ckId) throws ServiceException {
        //分配历史ID
        wmsDistributionHistory.setId(UUIDUtils.uuid());
        //所属发运订单ID
        wmsDistributionHistory.setSsfyddid(wmsSplit.getSsfyddid());
        //分配人
        wmsDistributionHistory.setCdr(name);
        //操作类型 0分配 1取消分配
        wmsDistributionHistory.setCzlx("0");
        //所属拆单ID
        wmsDistributionHistory.setSscdid(wmsSplit.getId());
        //所属发运明细ID
        wmsDistributionHistory.setSsfymxid(wmsSplit.getSsfymxid());
        //货品ID
        wmsDistributionHistory.setHpid(wmsSplit.getHpid());
        //库存ID
        wmsDistributionHistory.setKcid(wmsStocks.getId());
        //分配数量
        wmsDistributionHistory.setFpsl(wmsStocks.getSl());
        //分配序号
        wmsDistributionHistory.setFpxh("1");
        wmsDistributionHistory.setCkid(ckId);
        Integer fplStj = dao.baseInsertDetail(wmsDistributionHistory);
        if (fplStj <= 0) {
            throw new ServiceException("分配历史信息添加失败");
        }
    }

    /**
     * 通用
     * @param wmsSplit
     * @param listStroke
     * @param listStrokes
     * @param wmsOwner
     * @param bjZy
     * @param wmsDistribution
     * @param wmsDistributionHistory
     * @param user
     * @param ckId
     * @param fpZsl
     * @return
     * @throws ServiceException
     */
    public Map<String, String> searchOne(WmsSplit wmsSplit, List<WmsStock> listStroke,List<WmsStock>
            listStrokes,WmsOwner wmsOwner,String bjZy,WmsDistribution wmsDistribution,WmsDistributionHistory wmsDistributionHistory,User user,String ckId,Integer fpZsl) throws ServiceException{
        Map<String ,String> map=new HashMap<>();
        //库存数量对象
        listStrokes.clear();
        //箱主单位数量(包装明细数据)
        Integer yqSl = 0;
        //此货品库存总数量(一条发运订单明细下的分配总的数量)
        Integer zsl = 0;
        //主单位数量
        yqSl = Integer.parseInt(wmsSplit.getSl());
        //用于标记,如果为0就是库存数量不够,如果为1就是库存数量足够
        String bj = "0";
        for (WmsStock wmsStock : listStroke) {
            //可用数量=库存总数量-冻结数量-待拣货数量-待补货数量-待转移数量
            Integer kySl = Integer.parseInt(wmsStock.getSl()) - Integer.parseInt(wmsStock.getDjsl()) - Integer.parseInt(wmsStock.getDxjsl()) - Integer.parseInt(wmsStock.getBhsl()) - Integer.parseInt(wmsStock.getDzysl());
            //叠加可用数量
            zsl = zsl + kySl;
            //如果总数量大于或者等于主单位数量
            if (zsl.equals(yqSl) || zsl > yqSl) {
                fpZsl = fpZsl + yqSl;
                //主单位数量-总数量备份+原来的带下架数量(待拣货数量)=带下架数量(待拣货数量)
                String dxJsl = String.valueOf(yqSl + Integer.parseInt(wmsStock.getDxjsl()));
                wmsStock.setDxjsl(dxJsl);
                wmsStock.setSl(String.valueOf(yqSl));
                //分配库存的对象
                listStrokes.add(wmsStock);
                //用于标记,如果为0就是库存数量不够,如果为1就是库存数量足够
                bj = "1";
                //库存数量大于或等于主单位数量,退出循环
                break;
            } else if (zsl < yqSl) {
                fpZsl = fpZsl + kySl;
                //用于标记,如果为0就是库存数量不够,如果为1就是库存数量足够
                bj = "0";
                //如果总数量小于主单位数量
                //可用数量+原来的带下架数量(待拣货数量)=带下架数量(待拣货数量)=库存数量
                String dxJsl = String.valueOf(kySl + Integer.parseInt(wmsStock.getDxjsl()));
                wmsStock.setDxjsl(dxJsl);
                wmsStock.setSl(String.valueOf(kySl));
                //分配库存的对象
                listStrokes.add(wmsStock);
            }
        }
        //库存数量不足时,查看货主表(数量不足是否允许出库,0数量不足,1数量足够)
        if ("0".equals(bj)) {
            //查询货主表(数量不足是否允许出库 0不允许 1允许)
            if ("0".equals(wmsOwner.getSlbuck())) {
                throw new ServiceException("库存数量不足,不允许出库");
            } else {
                //1、部分分配2、全部分配
                //用于判断发运订单以及明细状态(1部分分配或者2全部分配)
                bjZy = "1";
            }
        } else {
            //1、部分分配2、全部分配
            bjZy = "2";
        }
        //更改拆单的状态
        Integer ggCd = dao.updateWmsSplit(wmsSplit.getId(), bjZy);
        if (ggCd <= 0) {
            throw new ServiceException("拆单状态更改失败");
        }
        for (WmsStock wmsStocks : listStrokes) {
            //修改库存表待拣货数量(待下架数量)
            Integer jhSl = dao.updateWmsStock(wmsStocks);
            if (jhSl <= 0) {
                throw new ServiceException("库存表待拣货数量更改失败");
            }
            //分配实体类
            wmsDistribution = new WmsDistribution();
            //添加分配表数据
            this.baseInsert(wmsDistribution, wmsSplit, wmsStocks, ckId);
            //分配历史实体类
            wmsDistributionHistory = new WmsDistributionHistory();
            //添加分配历史表数据
            this.baseInsertDetail(wmsDistributionHistory, wmsSplit, wmsStocks, user.getName(), ckId);
        }
        String zsLs = String.valueOf(fpZsl);
        map.put("bjZy", bjZy);
        map.put("fpZsl",zsLs);
        return map;
    }


    /**
     *
     * @param list
     * @param userinfo
     * @param hzid
     */
    @BaseServiceDescribe(moduleName = "取消分配操作",btnName = "取消分配操作")
    @Transactional
    public void cancelDistribution(List<String> list,UserInfo userinfo,String hzid) throws Exception{
        //仓库编号
        String ckId=String.valueOf(userinfo.getUser().getOrgId().get(0));
        //分配人
        User user=userinfo.getUser();
        //修改发运订单状态修改为“初始”

        //在分配历史表记录取消信息
        for(String id : list) {
            WmsShipmentOrder wmsShipmentOrder=dao.searchOrder(id);
            //初始
            String jhZt = "0";
            //未装箱
            String zxZt = "0";
            if (!jhZt.equals(wmsShipmentOrder.getJhzt())) {
                throw new ServiceException("请选择拣货状态为初始的数据");
            } else if (!zxZt.equals(wmsShipmentOrder.getZxzt())) {
                throw new ServiceException("请选择装箱状态为未装箱的数据");
            } else {
                //查询发运订单明细
                List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(wmsShipmentOrder.getId(),"");
                for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                    //查询拆单信息()
                    List<WmsSplit> listWmsSplit = dao.searchWmsSplit(wmsShipmentOrderDetailed.getId());
                    for (WmsSplit wmsSplit : listWmsSplit) {
                        //查询分配仓库表
                        List<WmsDistribution> wmsDistributionList = dao.searchWmsStock_s(wmsSplit.getId(), hzid, ckId);
                        WmsDistributionHistory wmsDistributionHistory = null;
                        for (WmsDistribution wmsDistribution : wmsDistributionList) {
                            //库存内的待下架数量(待拣货数量)
                            String djHsl = wmsDistribution.getDjhsl();
                            if("".equals(djHsl) || djHsl==null){
                                djHsl="0";
                            }
                            //分配表里分配数量
                            String fpSl = wmsDistribution.getFpsl();
                            if("".equals(fpSl) || fpSl==null){
                                fpSl="0";
                            }
                            //库存内的待下架数量(待拣货数量)减去分配表里分配数量
                            Integer sl = Integer.parseInt(djHsl) - Integer.parseInt(fpSl);
                            //修改库存待拣货数量
                            Integer i = dao.updateKcSl(wmsDistribution.getKcid(), String.valueOf(sl));
                            if (i > 0) {
                                //删除分配表分配数据
                                i = dao.deleteFp(wmsDistribution.getId());
                                if (i > 0) {
                                    wmsDistributionHistory = new WmsDistributionHistory();
                                    wmsDistributionHistory.setId(UUIDUtils.uuid());
                                    //所属发运订单ID
                                    wmsDistributionHistory.setSsfyddid(wmsShipmentOrder.getId());
                                    //所属发运订单明细ID
                                    wmsDistributionHistory.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                    //分配人
                                    wmsDistributionHistory.setCdr(user.getName());
                                    //操作类型 0分配 1取消分配
                                    wmsDistributionHistory.setCzlx("1");
                                    //所属拆单ID
                                    wmsDistributionHistory.setSscdid(wmsSplit.getId());
                                    //货品ID
                                    wmsDistributionHistory.setHpid(wmsDistribution.getHpid());
                                    //库存ID
                                    wmsDistributionHistory.setKcid(wmsDistribution.getKcid());
                                    //分配数量
                                    wmsDistributionHistory.setFpsl(wmsDistribution.getFpsl());
                                    //分配序号
                                    wmsDistributionHistory.setFpxh("1");
                                    //仓库ID
                                    wmsDistributionHistory.setCkid(ckId);
                                    i = dao.baseInsertDetail(wmsDistributionHistory);
                                    if (i <= 0) {
                                        throw new ServiceException("取消分配失败");
                                    }
                                } else {
                                    throw new ServiceException("取消分配失败");
                                }
                            } else {
                                throw new ServiceException("取消分配失败");
                            }
                        }
                        //拆单表分配状态修改为0未分配
                        Integer i = dao.updateWmsSplit(wmsSplit.getId(), "0");
                        if (i <= 0) {
                            throw new ServiceException("取消分配失败");
                        }
                    }
                    //查询发运订单明细下的分配数量
                    String fpsl = dao.countFpsl(wmsShipmentOrderDetailed.getId());
                    if ("".equals(fpsl) || fpsl == null) {
                        fpsl = "0";
                    }
                    if ("".equals(wmsShipmentOrderDetailed.getFysl()) || wmsShipmentOrderDetailed.getFysl() == null) {
                        wmsShipmentOrderDetailed.setFysl("0");
                    }
                    /*//发运订单明细状态(0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成)
                    String fymxzt="0";
                    if(fpsl.equals(wmsShipmentOrderDetailed.getFysl())){
                        fymxzt="2";
                    }else if(fpsl.equals("0")){
                        fymxzt="0";
                    }else{
                        fymxzt="1";
                    }
                    //(分配）箱单位数量==>2代表箱;取包装明细箱类型的数量
                    WmsPackingDetailed wmsPackingDetailed = dao.searchPackingDetailed("2", wmsShipmentOrderDetailed.getHpid(), hzid);
                    //包装明细主单位类型的数量
                    WmsPackingDetailed wmsPackingDetailed0 = dao.searchPackingDetailed("0", wmsShipmentOrderDetailed.getHpid(), hzid);
                    String zdWsl = wmsPackingDetailed.getZdwsl();
                    Integer fpZsl=Integer.valueOf(fpsl);
                    //箱主单位整数（箱数）
                    Integer xdWzs = fpZsl / Integer.parseInt(zdWsl);
                    //箱主单位余数（个数）
                    Integer xdWys = fpZsl % Integer.parseInt(zdWsl);
                    String zdWsl2 = xdWzs + wmsPackingDetailed.getMs() + xdWys + wmsPackingDetailed0.getMs();*/
                    //修改发运订单明细的状态修改为“初始”以及（分配）箱单位数量(明细下的全部分配数据都取消,状态未初始0,箱单位数量改为0)
                    Integer i = dao.updateOrderDetail(wmsShipmentOrderDetailed.getId(), "0", "0", fpsl);
                    if (i <= 0) {
                        throw new ServiceException("取消分配失败");
                    }
                }
                /*//查询发运订单下的数据是否有部分分配数据
                Integer fyztsl1=dao.countFydd(wmsShipmentOrder.getId(),"1");
                //查询发运订单下的数据是否初始分配数据
                Integer fyztsl0=dao.countFydd(wmsShipmentOrder.getId(),"0");
                //查询发运订单下的数据是否全部分配数据
                Integer fyztsl2=dao.countFydd(wmsShipmentOrder.getId(),"2");
                //状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成)
                String fyzt="0";
                if(fyztsl1>0){
                    fyzt="1";
                }else if(fyztsl0>0){
                    if(fyztsl2>0){
                        fyzt="1";
                    }else{
                        fyzt="0";
                    }
                }*/
                //取消分配,全部取消订单下的全部分配数据(发运订单状态改为初始状态0)
                Integer i = dao.updateOrder(wmsShipmentOrder.getId(), "0", "", null, null, null);
                if (i<=0) {
                    throw new ServiceException("取消分配失败");
                }
            }
        }
        throw new SucessException("取消分配成功");
    }

    /**
     * 发运订单-追加分配管理
     *
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "追加分配操作", btnName = "追加分配操作")
    @Transactional
    public void addDistribution(List<String> list, UserInfo userinfo, String hzid) throws Exception {
        Map<String, String> map = new HashMap<>();
        //取出当前用户
        User user = userinfo.getUser();
        //分配总数量
        Integer fpZsl = 0;
        //仓库编号
        String ckId = String.valueOf(userinfo.getUser().getOrgId().get(0));
        //分配实体类
        WmsDistribution wmsDistribution = null;
        //分配明细(历史)实体类
        WmsDistributionHistory wmsDistributionHistory = null;
        //库存数量对象
        List<WmsStock> listStrokes = new ArrayList<>();
        //查询货主表(数量不足是否允许出库 0不允许 1允许)
        WmsOwner wmsOwner = dao.searchHzSlbuck(hzid);
        //查询周转规则排序(用货主编号通过货主与周转规则中间表排序显示规则字段)
        String zzGz = dao.searchZzgz(hzid, ckId);
        //标记专用(0,未进;1,进入)
        String bjzy = "0";
        //存储库位所对应的属性
        List<String> sxList = null;
        //存储库位所对应的类性
        List<String> lxList = null;
        //存储库位所对应的类型(第二种情况使用)
        List<String> lxList2 = null;
        //循环发运订单数据
        for (String id : list) {
            //查询发运订单数据
            WmsShipmentOrder wmsShipmentOrder = dao.searchOrder(id);
            //发运订单状态为部分分配
            String faZt = "1";
            if (!faZt.equals(wmsShipmentOrder.getFyzt())) {
                throw new ServiceException("请选择SO状态为部分分配的数据");
            } else {
                //查询发运订单明细
                List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(wmsShipmentOrder.getId(), "2");
                //循环发运订单明细数据
                for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                    //查询拆单信息()
                    List<WmsSplit> listWmsSplit = dao.searchWmsSplit(wmsShipmentOrderDetailed.getId());
                    //包装类型所对应的主单位
                    String zdWsl = null;
                    //循环拆单信息
                    for (WmsSplit wmsSplit : listWmsSplit) {
                        //判断库位是否为空(false为空)
                        boolean flagKw = false;
                        //判断批次是否为空(false为空)
                        boolean flagPc = false;
                        //判断生产日期是否为空(false为空)
                        boolean flagRq = false;
                        //存储库位所对应的属性
                        sxList = new ArrayList<>();
                        //判断库位是否为空
                        if (wmsSplit.getKwid() != null && !("").equals(wmsSplit.getKwid())) {
                            // 0正常 1封存 2管控 3禁入
                            sxList.add("0");
                            sxList.add("2");
                            sxList.add("3");
                            flagKw = true;
                        } else {
                            // 0正常 1封存 2管控 3禁入
                            sxList.add("0");
                            sxList.add("3");
                            flagKw = false;
                        }
                        //存储库位所对应的类型(4暂存库位 5集货库位 不可以分配)
                        lxList = new ArrayList<>();
                        //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
                        lxList.add("4");
                        lxList.add("5");
                        String pc = wmsSplit.getPc();
                        if (!"".equals(pc) && pc != null) {
                            flagPc = true;
                        }
                        String scrq = wmsSplit.getScrq();
                        if (!"".equals(scrq) && scrq != null) {
                            flagRq = true;
                        }
                        //判断库位,批次,生产日期是否为空
                        if (flagKw == true | flagPc == true | flagRq == true) {
                            //库位,批次,生产日期,如果有一个不为空,就执行下面的方法
                            List<WmsStock> listStroke = dao.searchWmsStock(wmsSplit.getKwid(), wmsSplit.getPc(), wmsSplit.getScrq(), wmsSplit.getHpid(), hzid, ckId, sxList, zzGz, lxList);
                            if (listStroke.size() > 0) {
                                map = searchOne(wmsSplit, listStroke, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                fpZsl = Integer.parseInt(map.get("fpZsl"));
                            } else {
                                //没有库存的时候跳出当前循环
                                continue;
                            }
                        } else {
                            //库位,批次,生产日期,如果都为空,就执行下面的方法
                            //分配规则(0:完全按照拣货库位类型;1:整箱优先找指定拣货库位类型);第一种情况
                            if ("0".equals(wmsOwner.getFpgz()) || !"2".equals(wmsSplit.getBzlx())) {
                                //查询拣货库位表对应的库位(货品ID,包装类型,货主实体类,仓库ID)
                                String jhBmx = jhBmx(wmsSplit.getBzlx());
                                List<WmsStock> wmsStockList = dao.searchKC3(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList);
                                if (wmsStockList.size() > 0) {
                                    map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                    fpZsl = Integer.parseInt(map.get("fpZsl"));
                                } else {
                                    //没有库存的时候跳出当前循环
                                    continue;
                                }
                            } else {
                                //查询拣货库位表对应的库位(货品ID,包装类型,货主实体类,仓库ID)第二种情况
                                String jhBmx = jhBmx(wmsSplit.getBzlx());
                                //查询货品对应的拣货库位类型(根据包装类型找到对应的拣货库位表,结合拆单表内的货品ID)
                                String kwlx = dao.searchJhlx(wmsSplit.getHpid(), ckId, jhBmx);
                                //库位类型集合
                                lxList2 = kwLx(kwlx, lxList2);
                                //查询该货品批次集合(存储库位,箱拣货库位,箱/件拣货库位,件拣货库位)
                                List<String> hppcList = dao.searchHppc(wmsSplit.getHpid(), hzid, ckId);
                                //拆单数量
                                Integer cdSl = Integer.valueOf(wmsSplit.getSl());
                                //累加库存数量时专用
                                Integer countList2 = 0;
                                go:
                                for (String hppc2 : hppcList) {
                                    //统计库存内的可用数量(指定库位类型)
                                    Integer countList1 = dao.countKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, kwlx);
                                    //判断指定库位类型的数量是否足够
                                    if (countList1 < cdSl) {
                                        //指定库位类型的数量不足时执行
                                        for (String lx : lxList2) {
                                            Integer countList = dao.countKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, lx);
                                            if (countList < cdSl) {
                                                //单个库位类型下面的库存数量不足,继续循环下一种库位类型
                                                //所有库位类型库存数量叠加
                                                countList1 += countList;
                                            } else {
                                                //库位类型下面的库存数量足够,执行
                                                List<WmsStock> wmsStockList = dao.searchKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, lx);
                                                if (wmsStockList.size() > 0) {
                                                    map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                                    fpZsl = Integer.parseInt(map.get("fpZsl"));
                                                    break go;
                                                }
                                            }
                                        }
                                        countList2 += countList1;
                                    } else {
                                        //指定库位类型的数量足够时执行
                                        List<WmsStock> wmsStockList = dao.searchKC(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppc2, kwlx);
                                        if (wmsStockList.size() > 0) {
                                            map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                            fpZsl = Integer.parseInt(map.get("fpZsl"));
                                            break go;
                                        }
                                    }
                                }
                                if (countList2 < cdSl) {
                                    //所有货品批次,库位类型内的库存数量都不足时执行
                                    if ("0".equals(wmsOwner.getSlbuck())) {
                                        throw new ServiceException("库存数量不足,不允许出库");
                                    } else {
                                        //数量不足,允许出库(进行分配)
                                        List<WmsStock> wmsStockList = dao.searchKC4(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppcList, lxList2);
                                        if (wmsStockList.size() > 0) {
                                            map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                            fpZsl = Integer.parseInt(map.get("fpZsl"));
                                        }
                                    }
                                } else {
                                    //所有货品批次,库位类型内的库存数量都足够时执行
                                    //数量足够,允许出库(进行分配)
                                    List<WmsStock> wmsStockList = dao.searchKC4(wmsSplit.getHpid(), jhBmx, hzid, ckId, zzGz, sxList, hppcList, lxList2);
                                    if (wmsStockList.size() > 0) {
                                        map = searchOne(wmsSplit, wmsStockList, listStrokes, wmsOwner, bjzy, wmsDistribution, wmsDistributionHistory, user, ckId, fpZsl);
                                        fpZsl = Integer.parseInt(map.get("fpZsl"));
                                    }
                                }
                            }
                        }
                    }
                    fpZsl = fpZsl + Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                    //(分配）箱单位数量==>2代表箱;取包装明细箱类型的数量
                    WmsPackingDetailed wmsPackingDetailed = dao.searchPackingDetailed("2", wmsShipmentOrderDetailed.getHpid(), hzid);
                    //包装明细主单位类型的数量
                    WmsPackingDetailed wmsPackingDetailed0 = dao.searchPackingDetailed("0", wmsShipmentOrderDetailed.getHpid(), hzid);
                    zdWsl = wmsPackingDetailed.getZdwsl();
                    //箱主单位整数（箱数）
                    Integer xdWzs = fpZsl / Integer.parseInt(zdWsl);
                    //箱主单位余数（个数）
                    Integer xdWys = fpZsl % Integer.parseInt(zdWsl);
                    String zdWsl2 = xdWzs + wmsPackingDetailed.getMs() + xdWys + wmsPackingDetailed0.getMs();
                    //发运订单明细状态
                    String ddmXzt = null;
                    //明细预期主单位数量
                    Integer mxYqSl = Integer.parseInt(wmsShipmentOrderDetailed.getYqsl());
                    //发运订单明细状态(0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成)
                    if ("1".equals(map.get("bjZy"))) {
                        if (fpZsl.equals(0)) {
                            //初始(没有库存是执行)
                            ddmXzt = "0";
                        } else if (fpZsl < mxYqSl) {
                            //部分分配
                            ddmXzt = "1";
                        }
                    } else if ("2".equals(map.get("bjZy"))) {
                        if (fpZsl.equals(mxYqSl)) {
                            //全部分配
                            ddmXzt = "2";
                        }
                    } else if ("0".equals(map.get("bjZy"))) {
                        //初始
                        ddmXzt = "0";
                    }
                    Integer fymCzt = dao.updateOrderDetail(wmsShipmentOrderDetailed.getId(), ddmXzt, zdWsl2, String.valueOf(fpZsl));
                    if (fymCzt <= 0) {
                        throw new ServiceException("发运订单明细状态更改失败");
                    }
                }
                //0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成 9 代表空
                String ddZt = null;
                //修改发运单状态(传入发运订单ID),0、初始 1、部分分配2、全部分配 3、部分拣货4、全部拣货5、部分发运6、全部发运7、订单取消8、订单完成
                if ("1".equals(map.get("bjZy"))) {
                    ddZt = "1";
                } else {
                    ddZt = "2";
                }
                //查询分配是否多批次
                List<WmsDistribution> wmsDistributionList = dao.searchPcList(wmsShipmentOrder.getId(), hzid, ckId);
                //定义批次状态0：多批次 1：单批次
                String dpczt = "0";
                if (wmsDistributionList.size() > 1) {
                    dpczt = "0";
                } else {
                    dpczt = "1";
                }
                //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                String jhrwzt = "0";
                //拣货状态（0、初始 1、部分拣货 2、全部拣货）
                String jhzt = null;
                //追加之前是全部拣货,改为部分拣货
                if ("2".equals(wmsShipmentOrder.getJhzt())) {
                    jhzt = "1";
                } else if ("0".equals(wmsShipmentOrder.getJhzt())) {
                    jhzt = "0";
                }
                //装箱状态（0、未装箱 1、部分装箱 2、全部装箱）
                String zxzt = null;
                //追加之前是全部装箱,改为部分装箱
                if ("2".equals(wmsShipmentOrder.getZxzt())) {
                    zxzt = "1";
                } else if ("0".equals(wmsShipmentOrder.getZxzt())) {
                    zxzt = "0";
                }
                Integer fyZt = dao.updateOrder(wmsShipmentOrder.getId(), ddZt, dpczt, jhrwzt, jhzt, zxzt);
                if (fyZt <= 0) {
                    throw new ServiceException("发运订单状态更改失败");
                }
            }
        }
        throw new SucessException("追加分配操作成功");
    }

    /**
     * 查看发运订单明细
     *
     * @param wmsShipmentOrderDetailed
     * @return
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "查看发运订单明细操作", btnName = "查看发运订单明细")
    public Page<WmsShipmentOrderDetailed> searchDetailed(WmsShipmentOrderDetailed wmsShipmentOrderDetailed) throws Exception {
        try {
            PageHelper.startPage(wmsShipmentOrderDetailed.getPage(), wmsShipmentOrderDetailed.getRows());
            return (Page<WmsShipmentOrderDetailed>) dao.searchDetailed(wmsShipmentOrderDetailed);
        } catch (Exception e) {
            throw new ServiceException("查看发运订单明细失败", e);
        }
    }

    /**
     * 查看分配明细
     *
     * @param wmsDistribution
     * @return
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "查看分配明细操作", btnName = "查看分配明细")
    public Page<WmsDistribution> distributionDetailed(WmsDistribution wmsDistribution) throws Exception {
        try {
            PageHelper.startPage(wmsDistribution.getPage(), wmsDistribution.getRows());
            return (Page<WmsDistribution>) dao.distributionDetailed(wmsDistribution);
        } catch (Exception e) {
            throw new ServiceException("查看分配明细失败", e);
        }
    }

    /**
     * 判断拣货库位
     *
     * @param bzlx
     * @return
     * @throws Exception
     */
    public String jhBmx(String bzlx) throws Exception {
        String jhBmx = null;
        switch (bzlx) {
            //主单位
            case "0":
                jhBmx = "wms_one_level_picking_location";
                break;
            //内包装
            case "1":
                jhBmx = "wms_two_level_picking_location";
                break;
            //箱
            case "2":
                jhBmx = "wms_three_level_picking_location";
                break;
            //托盘
            case "3":
                jhBmx = "wms_four_level_picking_location";
                break;
            default:
                throw new ServiceException("查询拣货库位表失败");
        }
        return jhBmx;
    }

    /**
     * 库位类型
     *
     * @param kwlx
     * @param lxList2
     * @return
     * @throws Exception
     */
    public List<String> kwLx(String kwlx, List<String> lxList2) throws Exception {
        lxList2 = new ArrayList<>();
        //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
        switch (kwlx) {
            //存储库位
            case "0":
                lxList2.add("2");
                lxList2.add("3");
                lxList2.add("1");
                lxList2.add("6");
                break;
            //件拣货库位
            case "1":
                lxList2.add("0");
                lxList2.add("2");
                lxList2.add("3");
                lxList2.add("6");
                break;
            //箱拣货库位
            case "2":
                lxList2.add("0");
                lxList2.add("3");
                lxList2.add("1");
                lxList2.add("6");
                break;
            //箱/件拣货库位
            case "3":
                lxList2.add("0");
                lxList2.add("2");
                lxList2.add("1");
                lxList2.add("6");
                break;
            default:
                throw new ServiceException("查询库位类型失败");
        }
        return lxList2;
    }

    /**
     * 查看分配明细记录
     *
     * @param wmsDistribution
     * @return
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "修改分配明细记录", btnName = "查看分配明细记录操作")
    public Page<WmsDistribution> searchDistributionDetailed(WmsDistribution wmsDistribution) throws Exception {
        try {
            PageHelper.startPage(wmsDistribution.getPage(), wmsDistribution.getRows());
            return (Page<WmsDistribution>) dao.searchDistributionDetailed(wmsDistribution);
        } catch (Exception e) {
            throw new ServiceException("查看分配明细记录", e);
        }
    }

    /**
     * 修改分配明细记录
     *
     * @param wmsDistribution 传分配ID(id),更改的数量(fpsl),拆单ID(sscdid)
     */
    @BaseServiceDescribe(moduleName = "修改分配明细记录", btnName = "修改分配明细记录操作")
    @Transactional
    public void updateDistributionDetailed(WmsDistribution wmsDistribution, String hzid) throws Exception {
        //查询分配数量
        WmsDistribution wmsDistribution1 = dao.searchFpSl(wmsDistribution.getId());
        //查询库存待下架数量
        WmsStock wmsStock = dao.searchKc5(wmsDistribution1.getKcid());
        Integer dxjsl2 = Integer.valueOf(wmsStock.getDxjsl()) - Integer.valueOf(wmsDistribution1.getFpsl());
        //页面传入数量
        Integer sl = Integer.valueOf(wmsDistribution.getFpsl());
        //数据库分配数量
        Integer sl2 = Integer.valueOf(wmsDistribution1.getFpsl());
        if (sl > sl2) {
            throw new ServiceException("修改的分配数量不可大于原分配数量");
        } else if (sl.equals(sl2)) {
            //数量未修改
            throw new SucessException("修改的分配数量成功");
        } else {
            //包装明细主单位类型的数量
            WmsPackingDetailed wmsPackingDetailed0 = dao.searchPackingDetailed("0", wmsDistribution1.getHpid(), hzid);
            //总重量=分配数量*货品主单位毛重量
            BigDecimal totalWeight = java.math.BigDecimal.valueOf(Double.parseDouble(wmsDistribution1.getFpsl()) * Double.parseDouble(wmsPackingDetailed0.getZl()));
            wmsDistribution.setTotalWeight(totalWeight);
            //总体积=分配数量*货品主单位体积
            BigDecimal totalVolume = java.math.BigDecimal.valueOf(Double.parseDouble(wmsDistribution1.getFpsl()) * Double.parseDouble(wmsPackingDetailed0.getTj()));
            wmsDistribution.setTotalVolume(totalVolume);
            //分配数量
            Integer i = dao.updateDistributionSl(wmsDistribution);
            if (i > 0) {
                //更改库存待下架数量(待拣货数量)
                dxjsl2 += Integer.valueOf(wmsDistribution.getFpsl());
                i = dao.updateKcSl(wmsDistribution1.getKcid(), String.valueOf(dxjsl2));
                if (i > 0) {
                    //定义拆单的分配状态
                    String fpzt;
                    //查询拆单信息
                    WmsSplit wmsSplit = dao.searchWmsSplit2(wmsDistribution.getSscdid());
                    //分配数量为零
                    if ("0".equals(wmsDistribution.getFpsl()) || "".equals(wmsDistribution.getFpsl()) || wmsDistribution.getFpsl() == null) {
                        //0:是未分配
                        fpzt = "0";
                    } else if (wmsDistribution.getFpsl().equals(wmsSplit.getSl())) {
                        //2:是完全分配
                        fpzt = "2";
                    } else {
                        //1:是部分分配
                        fpzt = "1";
                    }
                    //更改拆单分配状态
                    i = dao.updateWmsSplit(wmsSplit.getId(), fpzt);
                    if (i > 0) {
                        //查询发运订单明细下的全部分配数量
                        WmsDistribution wmsDistribution2 = dao.searchFpSl2(wmsSplit.getSsfymxid());
                        //(分配）箱单位数量==>2代表箱;取包装明细箱类型的数量
                        WmsPackingDetailed wmsPackingDetailed = dao.searchPackingDetailed("2", wmsSplit.getHpid(), hzid);
                        //箱包装主单位数量
                        Integer zdWsl = Integer.parseInt(wmsPackingDetailed.getZdwsl());
                        //分配总数量
                        Integer fpZsl = Integer.valueOf(wmsDistribution2.getFpsl());
                        //箱主单位整数（箱数）
                        Integer xdWzs = fpZsl / zdWsl;
                        //箱主单位余数（个数）
                        Integer xdWys = fpZsl % zdWsl;
                        String zdWsl2 = xdWzs + wmsPackingDetailed.getMs() + xdWys + wmsPackingDetailed0.getMs();
                        String ddmXzt = null;
                        if ("".equals(wmsDistribution2.getFpsl()) || wmsDistribution2.getFpsl() == null || "0".equals(wmsDistribution2.getFpsl())) {
                            ddmXzt = "0";
                        } else {
                            ddmXzt = "1";
                        }
                        //查询发运订单明细数据(用于出错返回使用)
                        WmsShipmentOrderDetailed wmsShipmentOrderDetailed = dao.searchDdmx(wmsSplit.getSsfymxid());
                        //更改发运订单明细数据
                        i = dao.updateOrderDetail(wmsSplit.getSsfymxid(), ddmXzt, zdWsl2, String.valueOf(fpZsl));
                        if (i > 0) {
                            //查询发运订单下的全部分配数量
                            WmsDistribution wmsDistribution3 = dao.searchFpSl3(wmsSplit.getSsfyddid());
                            String ddZt = null;
                            if ("".equals(wmsDistribution3.getFpsl()) || wmsDistribution3.getFpsl() == null || "0".equals(wmsDistribution3.getFpsl())) {
                                ddZt = "0";
                            } else {
                                ddZt = "1";
                            }
                            //查询分配是否多批次
                            List<WmsDistribution> wmsDistributionList = dao.searchPcList(wmsSplit.getSsfyddid(), hzid, wmsSplit.getCkid());
                            //定义批次状态0：多批次 1：单批次
                            String dpczt = "0";
                            if (wmsDistributionList.size() > 1) {
                                dpczt = "0";
                            } else {
                                dpczt = "1";
                            }
                            //更改发运订单信息
                            i = dao.updateOrder(wmsSplit.getSsfyddid(), ddZt, dpczt, null, null, null);
                            if (i > 0) {
                                throw new SucessException("修改分配数量成功");
                            } else {
                                roolBack(wmsDistribution1, wmsStock, wmsSplit, wmsShipmentOrderDetailed);
                                throw new ServiceException("发运订单更改失败");
                            }
                        } else {
                            roolBack(wmsDistribution1, wmsStock, wmsSplit, null);
                            throw new ServiceException("发运订单明细更改失败");
                        }
                    } else {
                        roolBack(wmsDistribution1, wmsStock, null, null);
                        throw new ServiceException("修改拆单分配状态失败");
                    }
                } else {
                    roolBack(wmsDistribution1, null, null, null);
                    throw new ServiceException("修改库存待下架数量失败");
                }
            } else {
                throw new ServiceException("修改分配数量失败");
            }
        }
    }

    /**
     * 修改分配明细记录操作回滚
     *
     * @param wmsDistribution1
     * @param wmsStock
     * @param wmsSplit
     * @param wmsShipmentOrderDetailed
     */
    public void roolBack(WmsDistribution wmsDistribution1, WmsStock wmsStock, WmsSplit wmsSplit, WmsShipmentOrderDetailed wmsShipmentOrderDetailed) {
        //回滚分配
        if (wmsDistribution1 != null) {
            dao.updateDistributionSl(wmsDistribution1);
        }
        //回滚库存
        if (wmsStock != null) {
            dao.updateKcSl(wmsStock.getId(), wmsStock.getDxjsl());
        }
        //回滚拆单
        if (wmsSplit != null) {
            dao.updateWmsSplit(wmsSplit.getId(), wmsSplit.getFpzt());
        }
        //回滚明细
        if (wmsShipmentOrderDetailed != null) {
            dao.updateOrderDetail(wmsShipmentOrderDetailed.getId(), wmsShipmentOrderDetailed.getFymxzt(), wmsShipmentOrderDetailed.getFpxdwsl(), wmsShipmentOrderDetailed.getFpsl());
        }
    }
}
